<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>竞态问题</title>
</head>
<style>
    body {
        background: #f0f2f5;
        font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
        margin: 0;
        padding: 20px;
        color: #333;
    }

    .container {
        width: 100%;
        max-width: 900px;
        margin: 40px auto;
        user-select: none;
        background: white;
        border-radius: 16px;
        padding: 30px;
        box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
        box-sizing: border-box;
    }

    .cards {
        width: 100%;
        display: flex;
        justify-content: center;
        gap: 20px;
        margin-bottom: 30px;
    }

    .card {
        width: 180px;
        height: 70px;
        padding: 20px;
        background: #ffffff;
        margin: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 12px;
        font-size: 16px;
        font-weight: 500;
        color: #4a5568;
        box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
        transition: all 0.3s ease;
        border: 2px solid transparent;
        cursor: pointer;
    }

    .card:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
        border-color: #e2e8f0;
    }

    .active {
        background: #4299e1;
        color: white;
        border-color: transparent;
        box-shadow: 0 4px 15px rgba(66, 153, 225, 0.3);
    }

    .cards-content {
        width: auto;
        min-height: 200px;
        border-radius: 16px;
        background: linear-gradient(to bottom, #ffffff, #f8fafc);
        padding: 30px;
        font-size: 16px;
        line-height: 1.8;
        color: #2d3748;
        transition: all 0.3s ease;
        box-shadow:
            inset 0 2px 4px rgba(0, 0, 0, 0.03),
            0 1px 3px rgba(0, 0, 0, 0.05);
        position: relative;
        overflow: hidden;
        box-sizing: border-box;
        word-wrap: break-word;
        border: 1px solid rgba(226, 232, 240, 0.8);
        margin-top: 10px;
    }

    .cards-content::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        height: 3px;
        background: linear-gradient(90deg, #4299e1, #667eea);
        opacity: 0.8;
    }

    .cards-content:empty {
        display: flex;
        align-items: center;
        justify-content: center;
        color: #a0aec0;
        font-style: italic;
    }

    .cards-content:empty::after {
        content: '等待选择...';
    }

    @keyframes fadeIn {
        from {
            opacity: 0;
            transform: translateY(10px);
            filter: blur(5px);
        }
        to {
            opacity: 1;
            transform: translateY(0);
            filter: blur(0);
        }
    }

    .cards-content:not(:empty) {
        animation: fadeIn 0.4s ease-out;
    }
</style>
<body>
<div class="container">
    <div class="cards">
        <div class="card">card-first</div>
        <div class="card">card-second</div>
    </div>
    <div class="cards-content"></div>
</div>
</body>
<script>
    function getCardInfoAPI(target) {
        return new Promise((resolve) => {
            let random = Math.random() * 0.5 + 1;
            setTimeout(() => {
                resolve(target.innerHTML + ': ' + random + '秒返回')
            }, random * 1000)
        })
    }
</script>
<script>
    // 所有元素的对象
    const domObj = {
        cards: document.querySelector('.cards'),
        cardContent: document.querySelector('.cards-content')
    }
    changeInfo(domObj.cards.children[0])
    domObj.cards.addEventListener('click', function (e) {
        e.preventDefault()
        if (e.target.classList.contains('card')) {
            document.querySelector('.active').classList.remove('active')
            changeInfo(e.target)
        }
    })

    function changeInfo(target) {
        target.classList.add('active')
        getCardInfoAPI(target).then(resp => {
            console.log('赋值成功')
            domObj.cardContent.innerHTML = resp
        }).catch(() => {
            console.log('赋值失败')
        })
    }
</script>
</html>
